package com.bykj.property;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author yiming

 * @projectName report_server
 * @description: TODO
 * @date 2019/5/2410:35
 */
public class Generator {
public static String scanner(String tip){
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入"+tip+":");
        System.out.println(help.toString());
        if(scanner.hasNext()){
        String ipt = scanner.next();
        if(StringUtils.isNotEmpty(ipt)){
        return ipt;
        }
        }
        throw new MybatisPlusException("请输入正确的"+tip+"!");
        }

public static void main(String[] args) {
        //代码生成器
        AutoGenerator mpg = new AutoGenerator();

        //全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath+"/src/main/java");//生成目文件的输出目录
        gc.setAuthor("liangyiming");//开发人员
        gc.setOpen(false);//文件生成后，是否需要打开所在路径
        gc.setFileOverride(false);//是否覆盖已有文件
        gc.setEnableCache(false);//是否在xml中添加二级缓存设置
        gc.setActiveRecord(false);//开启ActiveRecord模式
        gc.setBaseResultMap(true);//开启BaseResultMap
        gc.setBaseColumnList(true);//开启BaseColumnList
        gc.setDateType(DateType.SQL_PACK);//时间类型对应策略

        gc.setSwagger2(true);
        gc.setControllerName("%sController");
        gc.setEntityName("%s");//实体命名方式
        gc.setServiceName("%sService");
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        gc.setIdType(IdType.ID_WORKER_STR);

        mpg.setGlobalConfig(gc);

        //数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://212.64.112.244:3306/property?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("bykG@six.");
        mpg.setDataSource(dsc);



        //包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.bykj.property");//父包名，如果为空，将下面子包名必须写全部，否则就只需写子包名
        // pc.setModuleName(scanner("模块名"));//父包模块名称
        pc.setEntity("model");

        pc.setController("controller");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        pc.setXml("mapping");
        mpg.setPackageInfo(pc);

        String pagePath= projectPath+"/src/main/java/com/bykj/property/";
        //自定义配置
        InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {

        }
        };
        // 如果模板引擎是 freemarker
        //自定义输出配置
        List<FileOutConfig> fileOutConfigs = new ArrayList<>();
        //自定义配置会被优先输出
        String templatePathCon = "/templates/controller/t_controller.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templatePathCon) {
@Override
public String outputFile(TableInfo tableInfo) {
        //自定义输出文件名

        String expand =pagePath+"controller";
        String entityFile = String.format((expand + File.separator + "%s" + ".java"), tableInfo.getControllerName());
        return entityFile;

        }
        });
        String templateApiController = "/templates/api/controller/t_apicontroller.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templateApiController) {
@Override
public String outputFile(TableInfo tableInfo) {
        //自定义输出文件名

        String expand =pagePath+"api/controller";
        String entityFile = String.format((expand + File.separator + "%s" + "ApiController.java"), tableInfo.getEntityName());
        return entityFile;

        }
        });
        String templateMapper = "/templates/mapper/t_Mapper.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templateMapper){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"mapper";
        String entityFile = String.format((expand + File.separator + "%s" + "Mapper.java"), tableInfo.getEntityName());
        return entityFile;
        }
        });
        String templateEntity = "/templates/model/t_Entity.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templateEntity){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"model";
        String entityFile = String.format((expand + File.separator + "%s" + ".java"), tableInfo.getEntityName());
        return entityFile;
        }
        });
       /* String templateEntityEx = "/templates/model/entity/t_EntityEx.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templateEntityEx){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"model/entity";
        String entityFile = String.format((expand + File.separator + "%s" + "Entity.java"), tableInfo.getEntityName());
        return entityFile;
        }
        });*/
        String templateService = "/templates/service/t_service.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templateService){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"service";
        String entityFile = String.format((expand + File.separator + "%s" + "Service.java"), tableInfo.getEntityName());
        return entityFile;
        }
        });

        String templateServiceImpl = "/templates/service/impl/t_serviceimpl.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templateServiceImpl){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"service/impl";
        String entityFile = String.format((expand + File.separator + "%s" + "ServiceImpl.java"), tableInfo.getEntityName());
        return entityFile;
        }
        });
        String templatePathPage = "/templates/controller/vo/request/t_pageRequest.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templatePathPage){
@Override
public String outputFile(TableInfo tableInfo) {

        String expand = pagePath+"controller/vo/request";
        String entityFile = String.format((expand + File.separator + "%s" + "PageRequest.java"), tableInfo.getEntityName());
        return entityFile;
        }
        });
        String templatePathErr = "/templates/controller/vo/response/ErrorCode.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templatePathErr){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand = pagePath+"controller/vo/response";
        String entityFile = expand+"/ErrorCode.java";
        return entityFile;
        }
        });
        String templatePathRes = "/templates/controller/vo/response/RespResult.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templatePathRes){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"controller/vo/response";
        String entityFile = expand+"/RespResult.java";
        return entityFile;
        }
        });
        String templatePathEasyUtil = "/templates/util/ExcelUtils.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templatePathEasyUtil){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"util";
        String entityFile = expand+"/ExcelUtils.java";
        return entityFile;
        }
        });
        String templatePathIpUtil = "/templates/util/IPUtil.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templatePathIpUtil){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"util";
        String entityFile = expand+"/IPUtil.java";
        return entityFile;
        }
        });
        String templatePathHttpContextUtil = "/templates/util/HttpContextUtil.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templatePathHttpContextUtil){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"util";
        String entityFile = expand+"/HttpContextUtil.java";
        return entityFile;
        }
        });
        String templatePathAddrUtil = "/templates/util/AddressUtil.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templatePathAddrUtil){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"util";
        String entityFile = expand+"/AddressUtil.java";
        return entityFile;
        }
        });
        String templatePathEasyVo = "/templates/controller/vo/export/t_easypoiVo.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templatePathEasyVo){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"controller/vo/export";
        String entityFile = String.format((expand + File.separator + "%s" + "Export.java"), tableInfo.getEntityName());
        return entityFile;
        }
        });

        String templatePathModelView = "/templates/controller/vo/response/view/t_responsevo.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templatePathModelView){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"controller/vo/response/view";
        String entityFile = String.format((expand + File.separator + "%s" + "Vo.java"), tableInfo.getEntityName());
        return entityFile;
        }
        });
        String templatePathExpection = "/templates/config/common/exception/CustomException.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templatePathExpection){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"config/common/exception";
        String entityFile = expand+"/CustomException.java";
        return entityFile;
        }
        });
        String templatePathWebLog = "/templates/config/common/aspect/WebLogAspect.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templatePathWebLog){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"config/common/aspect";
        String entityFile = expand+"/WebLogAspect.java";
        return entityFile;
        }
        });
        String templatePathlog = "/templates/config/common/annotation/Log.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templatePathlog){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"config/common/annotation";
        String entityFile = expand+"/Log.java";
        return entityFile;
        }
        });
        String templatePathlogasp = "/templates/config/common/aspect/LogAspect.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templatePathlogasp){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"config/common/aspect";
        String entityFile = expand+"/LogAspect.java";
        return entityFile;
        }
        });
        String templatePathLogService = "/templates/sys/service/LogService.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templatePathLogService){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"sys/service";
        String entityFile = expand+"/LogService.java";
        return entityFile;
        }
        });
        String templatePathLogServiceImpl = "/templates/sys/service/impl/LogServiceImpl.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templatePathLogServiceImpl){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"sys/service/impl";
        String entityFile = expand+"/LogServiceImpl.java";
        return entityFile;
        }
        });

        String templatePathHanle = "/templates/config/common/webApp/PortalTomcatWebServerCustomizer.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templatePathHanle){
                @Override
                public String outputFile(TableInfo tableInfo) {
                        String expand =pagePath+"config/common/webApp";
                        String entityFile = expand+"/PortalTomcatWebServerCustomizer.java";
                        return entityFile;
                }
        });
        String templateMybatisPuls = "/templates/config/mybatisplus/MybatisPlusConfig.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templateMybatisPuls){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"config/mybatisplus";
        String entityFile = expand+"/MybatisPlusConfig.java";
        return entityFile;
        }
        });
        String templateSwagger = "/templates/config/swagger/SwaggerConfig.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templateSwagger){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"config/swagger";
        String entityFile = expand+"/SwaggerConfig.java";
        return entityFile;
        }
        });
        String templateshiroconfigon = "/templates/config/shiro/ShiroConfiguration.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templateshiroconfigon){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"config/shiro";
        String entityFile = expand+"/ShiroConfiguration.java";
        return entityFile;
        }
        });
        String templateshiroreal = "/templates/config/shiro/MyShiroRealm.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templateshiroreal){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"config/shiro";
        String entityFile = expand+"/MyShiroRealm.java";
        return entityFile;
        }
        });
        String templateshirofilter = "/templates/config/shiro/filter/ShiroLoginFilter.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templateshirofilter){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"config/shiro/filter";
        String entityFile = expand+"/ShiroLoginFilter.java";
        return entityFile;
        }
        });
        String templateloginController = "/templates/sys/controller/LoginController.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templateloginController){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"sys/controller";
        String entityFile = expand+"/LoginController.java";
        return entityFile;
        }
        });
        String templateloginService = "/templates/sys/service/LoginService.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templateloginService){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"sys/service";
        String entityFile = expand+"/LoginService.java";
        return entityFile;
        }
        });
        String templateloginServiceImpl = "/templates/sys/service/impl/LoginServiceImpl.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templateloginServiceImpl){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"sys/service/impl";
        String entityFile = expand+"/LoginServiceImpl.java";
        return entityFile;
        }
        });
        String templateCustomSessionManager = "/templates/config/shiro/CustomSessionManager.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templateCustomSessionManager){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"config/shiro";
        String entityFile = expand+"/CustomSessionManager.java";
        return entityFile;
        }
        });
        String templateWebMvcConfiguration = "/templates/config/common/webApp/WebMvcConfiguration.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templateWebMvcConfiguration){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"config/common/webApp";
        String entityFile = expand+"/WebMvcConfiguration.java";
        return entityFile;
        }
        });
        String templateSysMenuDao = "/templates/sys/dao/SysMenuDao.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templateSysMenuDao){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"sys/dao";
        String entityFile = expand+"/SysMenuDao.java";
        return entityFile;
        }
        });
        String templateSysRoleDao = "/templates/sys/dao/SysRoleDao.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templateSysRoleDao){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"sys/dao";
        String entityFile = expand+"/SysRoleDao.java";
        return entityFile;
        }
        });
        String templateSysUserDao = "/templates/sys/dao/SysUserDao.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templateSysUserDao){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"sys/dao";
        String entityFile = expand+"/SysUserDao.java";
        return entityFile;
        }
        });
        String templateValidateDao = "/templates/sys/dao/ValidateDao.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templateValidateDao){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"sys/dao";
        String entityFile = expand+"/ValidateDao.java";
        return entityFile;
        }
        });
        String templateValidateService = "/templates/sys/service/ValidateService.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templateValidateService){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"sys/service";
        String entityFile = expand+"/ValidateService.java";
        return entityFile;
        }
        });
        String templateValidateServiceImpl = "/templates/sys/service/impl/ValidateServiceImpl.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templateValidateServiceImpl){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"sys/service/impl";
        String entityFile = expand+"/ValidateServiceImpl.java";
        return entityFile;
        }
        });
        String templateFileController = "/templates/sys/controller/FileController.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templateFileController){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"sys/controller";
        String entityFile = expand+"/FileController.java";
        return entityFile;
        }
        });
        String templateSysVoFileInfo = "/templates/sys/controller/vo/FileInfo.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templateSysVoFileInfo){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"sys/controller/vo";
        String entityFile = expand+"/FileInfo.java";
        return entityFile;
        }
        });
        String templateSysVoValidateInfo = "/templates/sys/controller/vo/ValidateInfo.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templateSysVoValidateInfo){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"sys/controller/vo";
        String entityFile = expand+"/ValidateInfo.java";
        return entityFile;
        }
        });
        String templateValidateController = "/templates/sys/controller/ValidateController.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templateValidateController){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"sys/controller";
        String entityFile = expand+"/ValidateController.java";
        return entityFile;
        }
        });
        String templateRandomUtil = "/templates/util/RandomUtil.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templateRandomUtil){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"util";
        String entityFile = expand+"/RandomUtil.java";
        return entityFile;
        }
        });
        String templateMd5HashUtil = "/templates/util/Md5HashUtil.java.ftl";
        fileOutConfigs.add(new FileOutConfig(templateMd5HashUtil){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"util";
        String entityFile = expand+"/Md5HashUtil.java";
        return entityFile;
        }
        });
        String templatet_mapping = "/templates/mapping/t_mapping.xml.ftl";
        fileOutConfigs.add(new FileOutConfig(templatet_mapping){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+ "mapping";
        String entityFile = String.format((expand + File.separator + "%s" + "Mapper.xml"), tableInfo.getEntityName());
        return entityFile;
        }
        });
        String templateViewVue = "/templates/view/view.vue.ftl";
        fileOutConfigs.add(new FileOutConfig(templateViewVue){
@Override
public String outputFile(TableInfo tableInfo) {
        String expand =pagePath+"view";
        String dirEn=String.format((expand + File.separator + "%s" ), tableInfo.getName());
        String entityFile = String.format((dirEn + File.separator + "%s" + ".vue"), tableInfo.getEntityName());
        return entityFile;
        }
        });
        cfg.setFileOutConfigList(fileOutConfigs);
        mpg.setCfg(cfg);
        cfg.setFileOutConfigList(fileOutConfigs);//配置FileOutConfig指定模板文件、输出文件达到自定义文件生成目的
        mpg.setCfg(cfg);
        // mpg.setTemplate(new TemplateConfig().setXml(null));

        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setCapitalMode(true)
        //这里结合Lombok使用，有true,无false
        .setEntityLombokModel(true)
        .setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        //strategyConfig.setSuperEntityClass("com.baomidou.mybatisplus.extension.activerecord.Model");
        strategyConfig.setRestControllerStyle(true);
        // strategyConfig.setSuperControllerClass("com.wywl.mw.report.controller.base.BaseController");

        //strategyConfig.setInclude();
        strategyConfig.setSuperEntityColumns("id");
        strategyConfig.setControllerMappingHyphenStyle(true);
        strategyConfig.setTablePrefix(pc.getModuleName()+"_");

        mpg.setStrategy(strategyConfig);

        mpg.setTemplateEngine(new FreemarkerTemplateEngine());

        mpg.execute();


        }
        }
